//
//  WBMainViewController.swift
//  WeiBo

//

import UIKit

class WBMainViewController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        //1. 子视图
        setupAllViewControllers()
        //2. 监听登录
        loginNotificationCenter()
    }
    
    /// 监听登录
    private func loginNotificationCenter() {
        //注册通知
        NotificationCenter.default.addObserver(self, selector: #selector(login), name: NSNotification.Name(rawValue:loginNotification), object: nil)
    
    }
    
    @objc private func login() {
    
    
    
    }
    
}

//MARK: 设置子控制器
extension WBMainViewController {
    
    /// 创建所有子视图控制器
    fileprivate func setupAllViewControllers() {
        //1. item数组信息
        let homeDict = itemDict(itemIndex: 0)
        let messageDict = itemDict(itemIndex: 1)
        let discoverDict = itemDict(itemIndex: 2)
        let profileDict = itemDict(itemIndex: 3)
        let itemArray = [homeDict, messageDict, discoverDict, profileDict]
        
        //2.创建子控制器
        var vcArray = [UIViewController]()
        for dict in itemArray {
            let vc = controller(dict: dict)
            vcArray .append(vc)
        }
        //3.给tabbar控制器管理
        viewControllers = vcArray
    }
    
    /// 根据tabBar放置位置创建信息字典
    ///
    /// - Parameter itemIndex: 放置位置
    /// - Returns: 信息字典
    private func itemDict(itemIndex: Int) ->[String:AnyObject] {
        if itemIndex == 0 {
            return dict(clsName: "WBHomeViewController", imageName: "home", title: "首页")
        }
        if itemIndex == 1 {
            return dict(clsName: "WBMessageViewController", imageName: "message_center", title: "消息")
        }
        if itemIndex == 2 {
            return dict(clsName: "WBDiscoverViewController", imageName: "discover", title: "发现")
        }
        if itemIndex == 3 {
            return dict(clsName: "WBProfileViewController", imageName: "profile", title: "我")
        }
        return Dictionary()
    }
    
    /// 构建子控制器信息字典
    ///
    /// - Parameters:
    ///   - clsName: 控制器名
    ///   - imageName: TabbatItem显示图
    ///   - title: TabbatItem 名
    /// - Returns: 信息字典
    private func dict(clsName: String, imageName: String, title:String) ->[String: AnyObject] {
    let dict = ["clsName": clsName, "imageName": imageName, "title": title]
    return dict as [String : AnyObject]
    }
    
    /// 根据一个字典创建一个控制器
    ///
    /// - Parameter dict: 信息字典
    /// - Returns: 视图控制器
    private func controller(dict: [String: AnyObject]) -> UIViewController {
        //1.取到字典
        guard let clsName = dict["clsName"] as? String,
            let title = dict["title"] as? String,
            let imageName = dict["imageName"] as? String,
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName)as? WBBaseViewController.Type
//            let selectImg = UIImage(named: "tabbar_" + imageName + "_selected")
        else {
            return UIViewController()
        }
        //2.创建一个控制器
        let vc = cls.init()
        
        //3.标题与图片
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        //系统默认渲染色为蓝色，要渲染成图片本来的颜色，需更改渲染模式 alwaysOriginal
//        let img = selectImg.withRenderingMode(UIImageRenderingMode(rawValue: 1)!)
        /**
         封装方法
         */
        vc.tabBarItem.selectedImage = UIImage.renderingModeImage(named: "tabbar_" + imageName + "_selected", renderingMode: 1)
    
        // 4. 设置 tabbar 的标题字体（大小）
        vc.tabBarItem.setTitleTextAttributes(
            [NSForegroundColorAttributeName: UIColor.orange],
            for: .highlighted)
        // 系统默认是 12 号字，修改字体大小，要设置 Normal 的字体大小
        vc.tabBarItem.setTitleTextAttributes(
            [NSFontAttributeName: UIFont.systemFont(ofSize: 12)],
            for: UIControlState(rawValue: 0))
        
        //5. 导航控制器
        let navc = WBNavigationViewController(rootViewController: vc)
        
        return navc
    }


}
